<template>
  <div class="login">
    <div class="box">
      <div class="box-t">
        <img src="../assets/logo.png" alt />
      </div>
      <el-form
        ref="loginRef"
        :model="lomgfrom"
        label-width="0px"
        class="login_form"
        :rules="lomgfromres"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="lomgfrom.username" prefix-icon="el-icon-s-custom"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="lomgfrom.password"
            prefix-icon="el-icon-lock"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="longteng">登入</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      lomgfrom: {
        username: 'admin',
        password: '123456'
      },
      // 验证规则
      lomgfromres: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18个字符', trigger: 'blur' }
        ]
      },
      loginRef: {}
    }
  },
  created() {},
  methods: {
    longteng() {
      this.$refs.loginRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.lomgfrom)
        if (res.meta.status !== 200) return this.$message.error('登入失败')
        this.$message.success('登入成功')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  background: #2b4b6b;
  height: 100%;
  .box {
    width: 450px;
    height: 300px;
    border: 1px solid #eeeeee;
    border-radius: 5px;
    background: #ffffff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .box-t {
      width: 100px;
      height: 100px;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
    }
    .btns {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
